"""
初始化 | 行政区划信息
"""

import json

from my_typing import *


def _get_data_list_from_name(file_name: str) -> list[DictStrStr]:
    """ 根据文件名获取数据列表 """

    from config import settings

    with open(settings.PATH.DATA_DIR / "region" / file_name, "r", encoding="utf-8") as f:
        data_list: list[DictStrStr] = json.loads(f.read())

    return data_list


def init_province() -> None:
    """ 初始化 | 省 | 信息 """

    from components.logger import log
    from models.po import RegionProvincePO
    from service import RegionProvinceService

    log.success("[行政区划][省] 初始化 开始")

    po: RegionProvincePO | None = RegionProvinceService.find_one()

    if po is not None:
        log.info("[行政区划][省] 无需初始化")
        return None

    data_list: list[DictStrStr] = _get_data_list_from_name("provinces.json")

    po_list: list[RegionProvincePO] = [
        RegionProvincePO(
            code=_.get("code"),
            name=_.get("name"),
        )
        for _ in data_list
    ]

    RegionProvinceService.insert_many_with_pagination(po_list)

    log.success(f"[行政区划][省] 初始化 结束，共计 {len(data_list)} 条数据")


def init_city() -> None:
    """ 初始化 | 市 | 信息 """

    from components.logger import log
    from models.po import RegionCityPO
    from service import RegionCityService

    log.success("[行政区划][市] 初始化 开始")

    po: RegionCityPO | None = RegionCityService.find_one()

    if po is not None:
        log.info("[行政区划][市] 无需初始化")
        return None

    data_list: list[DictStrStr] = _get_data_list_from_name("cities.json")

    po_list: list[RegionCityPO] = [
        RegionCityPO(
            code=_.get("code"),
            name=_.get("name"),
            province_code=_.get("provinceCode"),
        )
        for _ in data_list
    ]

    RegionCityService.insert_many_with_pagination(po_list)

    log.success(f"[行政区划][市] 初始化 结束，共计 {len(data_list)} 条数据")


def init_area() -> None:
    """ 初始化 | 区 | 信息 """

    from components.logger import log
    from models.po import RegionAreaPO
    from service import RegionAreaService

    log.success("[行政区划][区] 初始化 开始")

    po: RegionAreaPO | None = RegionAreaService.find_one()

    if po is not None:
        log.info("[行政区划][区] 无需初始化")
        return None

    data_list: list[DictStrStr] = _get_data_list_from_name("areas.json")

    po_list: list[RegionAreaPO] = [
        RegionAreaPO(
            code=_.get("code"),
            name=_.get("name"),
            city_code=_.get("cityCode"),
            province_code=_.get("provinceCode"),
        )
        for _ in data_list
    ]

    RegionAreaService.insert_many_with_pagination(po_list)

    log.success(f"[行政区划][区] 初始化 结束，共计 {len(data_list)} 条数据")


def init_street() -> None:
    """ 初始化 | 街道 | 信息 """

    from components.logger import log
    from models.po import RegionStreetPO
    from service import RegionStreetService

    log.success("[行政区划][街道] 初始化 开始")

    po: RegionStreetPO | None = RegionStreetService.find_one()

    if po is not None:
        log.info("[行政区划][街道] 无需初始化")
        return None

    data_list: list[DictStrStr] = _get_data_list_from_name("streets.json")

    for i in data_list:
        if not i.get("cityCode") or not i.get("areaCode"):
            print(i)

    po_list: list[RegionStreetPO] = [
        RegionStreetPO(
            code=_.get("code"),
            name=_.get("name"),
            area_code=_.get("areaCode"),
            city_code=_.get("cityCode"),
            province_code=_.get("provinceCode"),
        )
        for _ in data_list
    ]

    RegionStreetService.insert_many_with_pagination(po_list)

    log.success(f"[行政区划][街道] 初始化 结束，共计 {len(data_list)} 条数据")


def init_region_data() -> None:
    """ 初始化 | 行政区划信息 """

    init_province()
    init_city()
    init_area()
    init_street()
